package cn.iocoder.yudao.module.biz.controller.admin.invoiceDict;

import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils;
import cn.iocoder.yudao.framework.excel.core.util.poi.ExcelUtilRuoyi;
import cn.iocoder.yudao.module.biz.controller.admin.invoice.domain.SanBizInvoiceContentDict;
import cn.iocoder.yudao.module.biz.controller.admin.invoice.service.SanBizInvoiceContentDictService;
import cn.iocoder.yudao.module.biz.controller.admin.pay.domain.SanBizPaymentManagement;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import org.springframework.web.multipart.MultipartFile;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 发票内容字典")
@RestController
@RequestMapping("/biz/invoice-content-dict")
@Validated
public class BizInvoiceContentDictController {

    @Resource
    private SanBizInvoiceContentDictService invoiceContentDictService;

    @PostMapping("/create")
    @Operation(summary = "创建发票内容字典")
    @PreAuthorize("@ss.hasPermission('biz:invoice-content-dict:create')")
    public CommonResult<Long> createInvoiceContentDict(@Valid @RequestBody SanBizInvoiceContentDict createReqVO) {
        invoiceContentDictService.save(createReqVO);
        return success(1l);
    }

    @PutMapping("/update")
    @Operation(summary = "更新发票内容字典")
    @PreAuthorize("@ss.hasPermission('biz:invoice-content-dict:update')")
    public CommonResult<Boolean> updateInvoiceContentDict(@Valid @RequestBody SanBizInvoiceContentDict updateReqVO) {
        invoiceContentDictService.updateById(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除发票内容字典")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('biz:invoice-content-dict:delete')")
    public CommonResult<Boolean> deleteInvoiceContentDict(@RequestParam("id") Long id) {
        invoiceContentDictService.removeById(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除发票内容字典")
                @PreAuthorize("@ss.hasPermission('biz:invoice-content-dict:delete')")
    public CommonResult<Boolean> deleteInvoiceContentDictList(@RequestParam("ids") List<Long> ids) {
        invoiceContentDictService.removeBatchByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得发票内容字典")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('biz:invoice-content-dict:query')")
    public CommonResult<SanBizInvoiceContentDict> getInvoiceContentDict(@RequestParam("id") Long id) {
        SanBizInvoiceContentDict invoiceContentDict = invoiceContentDictService.getById(id);
        return success(BeanUtils.toBean(invoiceContentDict, SanBizInvoiceContentDict.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得发票内容字典分页")
    @PreAuthorize("@ss.hasPermission('biz:invoice-content-dict:query')")
    public CommonResult<IPage<SanBizInvoiceContentDict>> getBizInvoiceContentPage(@Valid SanBizInvoiceContentDict pageReqVO) {
        IPage<SanBizInvoiceContentDict> pageResult = invoiceContentDictService.getBizContentDictPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出发票内容字典 Excel")
    @PreAuthorize("@ss.hasPermission('biz:invoice-content-dict:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInvoiceContentDictExcel(@Valid SanBizInvoiceContentDict pageReqVO,
              HttpServletResponse response) throws IOException {
        QueryWrapper queryWrapper = new QueryWrapper(pageReqVO);
        List<SanBizInvoiceContentDict> list = invoiceContentDictService.list(queryWrapper);
        // 导出 Excel
//        ExcelUtils.write(response, "发票内容字典.xls", "数据", SanBizInvoiceContentDict.class,
//                        BeanUtils.toBean(list, SanBizInvoiceContentDict.class));
        ExcelUtilRuoyi<SanBizInvoiceContentDict> util = new ExcelUtilRuoyi<>(SanBizInvoiceContentDict.class);
        util.exportExcel(response, list, "发票内容字典");
    }

    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入发票内容字典模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
//        HashMap<String,String[]> map = new HashMap<>();
//        String[] outDataType = DictFrameworkUtils.getDictDataLabelList("biz_pay_service_type").toArray(new String[0]);
//        if (outDataType!=null) {
//            map.put("biz_pay_service_type",outDataType);
//        }
        ExcelUtilRuoyi<SanBizInvoiceContentDict> util = new ExcelUtilRuoyi<>(SanBizInvoiceContentDict.class);
        util.importTemplateExcel(response, "打款记录");
    }

    @PostMapping("/import")
    @Operation(summary = "导入发票内容字典")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('pay:biz-payment-management:export')")
    public CommonResult<String> importExcel(@RequestParam("file") MultipartFile file,
                                            @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        ExcelUtilRuoyi<SanBizInvoiceContentDict> util = new ExcelUtilRuoyi<>(SanBizInvoiceContentDict.class);
        List<SanBizInvoiceContentDict> userList = util.importExcel(file.getInputStream());
        String message = invoiceContentDictService.importList(userList, updateSupport);
        return success(message);
    }
}